import axios from 'axios'
import http from '../../ajax/http'
import * as wzURL from '../../api'
const state = {
    tableData: [], //表格需要渲染的数据
    editArr: [],    //模态框渲染
    searchData: {}, //搜索出来的数据
    ceshi: "测试str"
}

const actions = {
    wzGetData({ commit }) {
        return http({
            method: 'post',
            // url: '/api/mock/24/getData',
            url: wzURL.wzGetData
        }).then(res => {
            // console.log(res);
            commit('addData', res.data.data)
        })
    },
    wzDelChangeData({ commit }, obj) {
        return http({
            url: wzURL.wzDelData,
            method: 'post',
            data: obj
        }).then(res => {
            console.log('操作成功得到响应');
            console.log(res.status);
            if (res.status === 200) {
                return http({
                    method: 'post',
                    url: wzURL.wzGetData
                }).then(res => {
                    console.log('重新请求得到响应,执行commit');
                    // console.log(res);
                    commit('addData', res.data.data)
                })
            } else {
                alert('删除失败')
            }
        })
    },
    wzSearch({ commit }, obj) {
        let { value, type } = obj
        console.log(obj);
        state.searchData = (state.tableData.filter(item => item[type] == value))
        console.log(state.searchData);
        if (state.searchData.length == 0) {
            state.searchData = {
                id: '未查询到相关数据',
                name: '未查询到相关数据',
                peopleName: '未查询到相关数据',
                phone: '未查询到相关数据',
                isdiscount: '未查询到相关数据',
                state: '未查询到相关数据', 
            }
        }
    }
}
const mutations = {
    addData(context, obj) {
        state.tableData = obj
    }
}

const getters = {
    getType(state) {
        return state.ceshi
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}







